$.ajaxSetup({
    dataType: 'jsonp',
});

var  marker=null;
var map = new AMap.Map('mapcontainer', {
    resizeEnable: true,
});
var  gaugeOptions={

    chart: {
        type: 'solidgauge'
    },
    credits: {
        enabled: false
    },
    title: null,

    pane: {
        center: ['50%', '85%'],
        size: '140%',
        startAngle: -90,
        endAngle: 90,
        background: {
            backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || '#EEE',
            innerRadius: '60%',
            outerRadius: '100%',
            shape: 'arc'
        }
    },

    tooltip: {
        enabled: false
    },

    // the value axis
    yAxis: {
        stops: [
            [0.1, '#55BF3B'], // green
            [0.5, '#DDDF0D'], // yellow
            [0.9, '#DF5353'] // red
            ],
        lineWidth: 0,
        minorTickInterval: null,
        tickAmount: 2,
        title: {
            y: -70
        },
        labels: {
            y: 16
        }
    },

    plotOptions: {
        solidgauge: {
            dataLabels: {
                y: 5,
                borderWidth: 0,
                useHTML: true
            }
        }
    }
}
const vo = new Vue({
    el:"#text",
    data: {
        jsession: null,
        isInitFinished: false,
        swfwidth: 0,//swf插件窗口宽度
        swfheight: 0,//swf 插件窗口高度
        chnCount: 4,//默认通道数
        serverIp: "",//请求的服务器ip
        serverPort: "",//请求的服务器端口
        isLoadLoginServer: false,
        fileList: [],
        timeoutId:0,
        model:{data:null},

    },
    created() {
        let data={"fuelWear":0,"fuelWears":0,"oilMass":0,"temp":0,"s":0,"fuelRate":0,"EngineTime":0,"pressure":0,"rpm":0,"mileage":0};
        this.model.data=data;
        this.init(data)
    },
    methods:{
        init(data){
            console.log(data);
            let chartSpeed = Highcharts.chart('container-speed', Highcharts.merge(gaugeOptions, {
                yAxis: {
                    min: 0,
                    max: 150,
                    title: {
                        text: '车速'
                    }
                },

                credits: {
                    enabled: false
                },

                series: [{
                    name: 'Speed',
                    data: [data.s],
                    dataLabels: {
                        format: '<div style="text-align:center"><span style="font-size:25px;color:' +
                        ((Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black') + '">{y}</span><br/>' +
                        '<span style="font-size:12px;color:silver">km/h</span></div>'
                    },
                    tooltip: {
                        valueSuffix: ' km/h'
                    }
                }]

            }));

// The RPM gauge
            let chartRpm = Highcharts.chart('container-rpm', Highcharts.merge(gaugeOptions, {
                yAxis: {
                    min: 0,
                    max: 5000,
                    title: {
                        text: '发动机转速'
                    }
                },

                series: [{
                    name: '转速',
                    data: [data.rpm],
                    dataLabels: {
                        format: '<div style="text-align:center"><span style="font-size:25px;color:' +
                        ((Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black') + '">{y:.1f}</span><br/>' +
                        '<span style="font-size:12px;color:silver">* r/pm</span></div>'
                    },
                    tooltip: {
                        valueSuffix: ' revolutions/min'
                    }
                }]

            }));

            let chartFuel = Highcharts.chart('container-fuel', Highcharts.merge(gaugeOptions, {
                yAxis: {
                    min: 0,
                    max: 30,
                    title: {
                        text: '实时油耗'
                    }
                },

                series: [{
                    name: '升/百公里',
                    data: [data.fuelWear],
                    dataLabels: {
                        format: '<div style="text-align:center"><span style="font-size:25px;color:' +
                        ((Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black') + '">{y:.1f}</span><br/>' +
                        '<span style="font-size:12px;color:silver">* 升/百公里</span></div>'
                    },
                    tooltip: {
                        valueSuffix: ' revolutions/min'
                    }
                }]

            }));

            let chartJiYou = Highcharts.chart('container-jiaolongspeed', Highcharts.merge(gaugeOptions, {
                yAxis: {
                    min: 0,
                    max: 800,
                    title: {
                        text: '机油压力'
                    }
                },

                series: [{
                    name: 'kpa',
                    data: [data.pressure],
                    dataLabels: {
                        format: '<div style="text-align:center"><span style="font-size:25px;color:' +
                        ((Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black') + '">{y:.1f}</span><br/>' +
                        '<span style="font-size:12px;color:silver">* r/pm</span></div>'
                    },
                    tooltip: {
                        valueSuffix: ' revolutions/min'
                    }
                }]
            }));
            this.model={chartSpeed,chartJiYou,chartFuel,chartRpm,data};
        },update(data){
            console.log(data);
            if(this.model!=null){
                this.model.chartFuel.series[0].points[0].update(data.fuelWear);
                this.model.chartSpeed.series[0].points[0].update(data.s);
                this.model.chartJiYou.series[0].points[0].update(data.pressure);
                this.model.chartRpm.series[0].points[0].update(data.rpm);
                this.model.data=data;
            }
        }
    }
})
/**
 * 用户登录
 * @param username
 * @param password
 */
var login = function (username, password) {
    if (username != null && password != null) {

        $.ajax(
            {
                type: 'get',
                url: `http://120.25.214.26/StandardApiAction_login.action?account=${username}&password=${password}`,
                success: function (data) {
                    let {jsession, result} = data;
                    if (result == 0) {
                        vo.jsession = jsession;
                        getLng();
                    } else {
                        alert(`登录失败错误代码:${result}`);
                    }
                },
                error: function () {
                    alert('登录失败');
                }
            }
        );
    }
}
/**
 * 用户注销
 * @param jsession
 */
var logout = function (jsession) {

    if (jsession) {
        $.ajax(
            {
                type: 'get',
                url: `http://120.25.214.26/StandardApiAction_logout.action?jsession=${jsession}`,
                success: function (data) {
                    console.log(data);
                }
            }
        );
    }
}

//获取gps信息
function getLng() {
    $.ajax(
        {
            type: 'get',
            url: `http://120.25.214.26/StandardApiAction_getDeviceStatus.action?jsession=${vo.jsession}&devIdno=50960&toMap=2`,
            success: function (data) {
                var gps = [parseFloat(data.status[0].mlng), parseFloat(data.status[0].mlat)];
                AMap.convertFrom(gps, 'baidu', function (status, info) {
                    if (info.info === 'ok') {
                        var lnglats = info.locations[0]; // Array.<LngLat>
                        onComplete(lnglats,data.status[0])
                    }
                });
            }
        }
    );
}

//解析定位结果
function onComplete(data,info) {
    console.log(data);
    if(marker!=null){
        marker.moveTo(data,60);
        marker.setAngle(info.hx)
    }else{
        marker = new AMap.Marker({
            icon: "../pages/img/car.png",
            map: map,
            position: data,
            autoRotation: true,
            label:{
                offset: new AMap.Pixel(-20,-30),
                content: "苏K1G101"
            }
        });
        vo.timeoutId=setInterval(function(){
            getLng();
        }, 2000);
    }

    //map.setCenter(data.position);

    marker.on("click", function () {
        $('#runmodal').modal({backdrop: 'static', keyboard: false});
        connect()
    })
/*    var str = ['定位成功'];
    str.push('经度：' + data.getLng());
    str.push('纬度：' + data.getLat());
    str.push('是否经过偏移：' + (data.isConverted ? '是' : '否'));
    document.getElementById('tip').innerHTML = str.join('<br>');*/
    map.setFitView();
    //map.setZoom(14);
}



